package com.erp.module.jiJian.shangChuanJiJian

import android.app.Activity
import androidx.appcompat.app.AlertDialog
import android.content.Intent
import android.view.KeyEvent
import android.view.View
import com.erp.activity.BaseActivity
import com.erp.activity.XiaoZuKActivity
import com.erp.myAdapter.XiaoZuKAdapter
import com.erp.bean.BenZuKBean
import com.erp.CaptureActivity
import com.erp.R
import com.erp.util.Dialogutil
import com.erp.util.Myutil
import com.erp.module.jiJian.JiJianContract
import kotlinx.android.synthetic.main.activity_sc_jijian.*
import kotlinx.android.synthetic.main.header_knoya.view.*
import java.util.*

/**
 * Created by Administrator on 2017/11/13/013.
 * 作者：刘洋
 */
class ShuangChuanJiJianKActivity : BaseActivity(),JiJianContract.ShuangChanJiJianKView, View.OnClickListener {

    lateinit var updialog: AlertDialog
    lateinit var benzulist : MutableList<BenZuKBean>
    lateinit var adapter : XiaoZuKAdapter
    lateinit var scjjkp : JiJianContract.ShuangChuanJiJianKP
    var data=""
    private val a = "ShuangChuanJiJianKActivity"
    override fun getLayoutId(): Int {
        activityname=a;
        return R.layout.activity_sc_jijian
    }

    override fun initCreate() {
        scjjkp= IShuangChuanJiJianKP(this, this)
        benzulist=ArrayList()
        adapter= XiaoZuKAdapter(benzulist, this)
        XiaoZULv.adapter=adapter
        ShiJianTv.setOnClickListener(this)
        LeftBt.setOnClickListener(this)
        JJSCHd.tv_header_title.text="计件扫描上传"
        JJSCHd.iv_header_left.visibility=View.VISIBLE
        JJSCHd.iv_header_left.setOnClickListener(this)
        RightBt.setOnClickListener(this)
        updialog = Myutil.showupdatedialog(this, "正在获取小组数据", false)
        scjjkp.getXiaoZu()
        JJSCHd.iv_header_left.isFocusable=true
        JJSCHd.iv_header_left.isFocusableInTouchMode=true
        JJSCHd.iv_header_left.requestFocus()
        JJSCHd.iv_header_left.requestFocusFromTouch()
    }
    override fun BenZuShuJuOk(xiaozuList: MutableList<BenZuKBean>,zuhao : String) {
        benzulist.clear()
        for(chengyuan in xiaozuList){
            benzulist.add(chengyuan)
        }
        adapter.notifyDataSetChanged()
        Myutil.settv(zuhao,ZuHaoTv)
        updialog.dismiss()
    }

    override fun BenZuShuJuNo(erroe: String) {
        Myutil.MyTost(erroe)
        updialog.dismiss()
    }

    override fun ShangChuanOk(data : String ) {
        Myutil.MyTost("上传成功")
        this.data=data
        updialog.dismiss()
    }

    override fun ShuangChuanNo(error : String) {
        Myutil.MyTost("长传失败")
        updialog.dismiss()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(data==null||resultCode!= Activity.RESULT_OK){
            return
        }
        when (requestCode) {
            1 -> {
                scjjkp.getXiaoZu()
                updialog = Myutil.showupdatedialog(this, "正在刷新", false)
            }
            2 -> {
                val bundle = data.extras
                val s = bundle?.get("result") as String
                scjjkp.setSaoMiao(ShiJianTv.text.toString(),JiHaoEt.text.toString(),ZuHaoEt.text.toString(),s)
                updialog = Myutil.showupdatedialog(this, "正在上传", false)
            }
        }
    }
    override fun onClick(v: View?) {
        when(v){
            LeftBt -> {
                val intent=Intent(this@ShuangChuanJiJianKActivity, CaptureActivity:: class.java)
                intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
                startActivityForResult(intent, 2)
            }
            RightBt -> {
                val intent= Intent(this@ShuangChuanJiJianKActivity, XiaoZuKActivity::class.java)
                startActivityForResult(intent,1)
            }
            JJSCHd.iv_header_left -> {
                var intent = Intent()
                intent.putExtra("data", data)
                setResult(Activity.RESULT_OK, intent)
                finish()
            }
            ShiJianTv -> {
                Dialogutil.showBottoPopupWindow(ShiJianTv,"计件时间","",this@ShuangChuanJiJianKActivity)
            }
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if(keyCode==4) {
            var intent = Intent()
            intent.putExtra("data", data)
            setResult(Activity.RESULT_OK, intent)
        }
        return super.onKeyDown(keyCode, event)
    }
}